﻿using System;
using System.Collections.Generic;
using System.Text;

namespace StockTrader.BusinessService.DataContract.HnxInfo
{
    public class HnxOrderData : IComparable
    {
        #region [Variables]
        public decimal OrderId { get; set; }
        public string OrderNo { get; set; }
        public DateTime OrderDate { get; set; }
        public string OrderTime { get; set; }
        public int MemberId { get; set; }
        public int StockId { get; set; }
        public string OrderType { get; set; }
        public string OrderSide { get; set; }
        public string OrderClass { get; set; }
        public string ModifiedStatus { get; set; }
        public int Settlement { get; set; }
        public string InvestorType { get; set; }
        public decimal OrderPrice { get; set; }
        public decimal OrderQtty { get; set; }
        public string Status { get; set; }
        public decimal QuotePrice { get; set; }
        public decimal QuoteQtty { get; set; }
        public decimal ExecQtty { get; set; }
        public decimal CorrectQtty { get; set; }
        public decimal CancelQtty { get; set; }
        public string AccountNo { get; set; }
        public int BrokerId { get; set; }
        public long TimeStamp { get; set; }

        #endregion

        public HnxOrderData()
        {

        }
        public HnxOrderData(decimal orderid, string orderno, DateTime orderdate, string time, decimal memberid,
                            decimal stockid, decimal ordertype, decimal norb, decimal norp, decimal norc, decimal setttype,
                            decimal dorf, decimal orderqtty, decimal orderprice, decimal status, decimal quoteprice,
                            decimal quoteqtty, decimal execqtty, decimal correcqtty, decimal cancelqtty,
                            string accno, decimal brokerid)
        {
            this.OrderId = orderid;
            this.OrderNo = orderno;
            this.OrderDate = orderdate;
            this.OrderTime = time;
            this.MemberId = Convert.ToInt32(memberid);
            this.StockId = Convert.ToInt32(stockid);
            switch (Convert.ToInt32(ordertype))
            {
                case 0:
                    this.OrderType = "Limit";
                    break;
                case 1:
                    this.OrderType = "Market";
                    break;
                case 2:
                    this.OrderType = "ATO";
                    break;
                default:
                    this.OrderType = "None";
                    break;
            }

            this.OrderSide = (norb.Equals(1)) ? "Buy" : "Sell";
            this.OrderClass = (norp.Equals(1)) ? "Normal" : "Negotiate";

            switch (Convert.ToInt32(norc))
            {
                case 2:
                    this.ModifiedStatus = "Normal";
                    break;
                case 5:
                    this.ModifiedStatus = "Origin";
                    break;
                case 6:
                    this.ModifiedStatus = "Wait";
                    break;
                case 7:
                    this.ModifiedStatus = "Modified";
                    break;
                case 8:
                    this.ModifiedStatus = "Cancelled";
                    break;
                case 9:
                    this.ModifiedStatus = "Matched";
                    break;
                case 10:
                    this.ModifiedStatus = "Quote";
                    break;
                case 11:
                    this.ModifiedStatus = "Deal";
                    break;
                default:
                    this.ModifiedStatus = "Unknown";
                    break;
            }
            this.Settlement = Convert.ToInt32(setttype);
            this.InvestorType = (dorf.Equals(1)) ? "Domestic" : "Foreign";
            this.OrderQtty = orderqtty;
            this.OrderPrice = orderprice;
            this.Status = status.Equals(1) ? "Normal" : "Unknown";
            this.QuotePrice = quoteprice;
            this.QuoteQtty = quoteqtty;
            this.AccountNo = accno;
            this.BrokerId = Convert.ToInt32(brokerid);
            this.ExecQtty = execqtty;
            this.CorrectQtty = correcqtty;
            this.CancelQtty = cancelqtty;
            this.TimeStamp = DateTime.Now.Ticks;
        }

        public HnxOrderData(decimal OrderId, string OrderNo, DateTime OrderDate, string OrderTime, int MemberId, int StockId, string OrderType, string OrderSide, string OrderClass, string ModifiedStatus, int Settlement, string InvestorType, decimal OrderPrice, decimal OrderQtty, string Status, decimal QuotePrice, decimal QuoteQtty, decimal ExecQtty, decimal CorrectQtty, decimal CancelQtty, string AccountNo, decimal BrokerId, long TimeStamp)
        {
            this.OrderId = OrderId;
            this.OrderNo = OrderNo;
            this.OrderDate = OrderDate;
            this.OrderTime = OrderTime;
            this.MemberId = MemberId;
            this.StockId = StockId;
            this.OrderType = OrderType;
            this.OrderSide = OrderSide;
            this.OrderClass = OrderClass;
            this.ModifiedStatus = ModifiedStatus;
            this.Settlement = Settlement;
            this.InvestorType = InvestorType;
            this.OrderPrice = OrderPrice;
            this.OrderQtty = OrderQtty;
            this.Status = Status;
            this.QuotePrice = QuotePrice;
            this.QuoteQtty = QuoteQtty;
            this.ExecQtty = ExecQtty;
            this.CorrectQtty = CorrectQtty;
            this.CancelQtty = CancelQtty;
            this.AccountNo = AccountNo;
            this.BrokerId = Convert.ToInt32(BrokerId);
            this.TimeStamp = TimeStamp;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            if ((OrderId - ((HnxOrderData)obj).OrderId).Equals(0))
            {
                if (OrderTime.Equals(((HnxOrderData)obj).OrderTime))
                    return this.QuotePrice.CompareTo(((HnxOrderData)obj).QuotePrice);
                else
                    return OrderTime.CompareTo(((HnxOrderData)obj).OrderTime);
            }
            else
                return OrderId.CompareTo(((HnxOrderData)obj).OrderId);
        }

        public override bool Equals(object obj)
        {
            if (OrderId.Equals(((HnxOrderData)obj).OrderId))
            {
                if (OrderTime.Equals(((HnxOrderData)obj).OrderTime))
                    return this.QuotePrice.Equals(((HnxOrderData)obj).QuotePrice);
                else
                    return OrderTime.Equals(((HnxOrderData)obj).OrderTime);
            }
            else
                return OrderId.Equals(((HnxOrderData)obj).OrderId);
        }
        #endregion
    }
}
